package org.itboys.mobile.controller.web.pay;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.pay.SundayPayConfig;
import org.itboys.mobile.entity.mysql.pay.SundayPayProduct;
import org.itboys.mobile.service.mysql.web.pay.SundayPayConfigService;
import org.itboys.mobile.service.mysql.web.pay.SundayPayProductService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 作者：江雄
 * 日期：2017年5月10日
 * 描述：圣代_支付_参数_controller_web
 */
@Controller
@RequestMapping("/sunday/web/pay/product")
public class SundayPayProductController extends BaseController{
    @Autowired
    private SundayPayProductService productService;

    /**
     * 主页
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        Model model){
            return "/sunday/pay/product/index";
    }


    /**
     * 查询积分配置
     * @param request
     * @param response
     */
    @RequestMapping("/select")
    public void select(HttpServletRequest request,
                       HttpServletResponse response){
        try{
            PageResult<SundayPayProduct> result=productService.selectConfig(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }

    }

    /**
     * 新增或者修改积分配置
     * @param request
     * @param response
     * @param product
     */
    @RequestMapping("/save")
    public void save(
            HttpServletRequest request,
            HttpServletResponse response,
            @ModelAttribute SundayPayProduct product,
            @RequestParam(value="imageFile",required=false) MultipartFile imageFile){
        try {
            productService.saveProduct(product,imageFile);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/pay/product/save------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(-1,e.getMessage());
        }
    }

    @RequestMapping("/findOne")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam(value="id",required = true)long id,
            @RequestParam(value="type",required = true)Integer type){
        try {
            SundayPayProduct product=productService.findOne(id,type);
            ResponseMessage.success(product);
        } catch (Exception e) {
            e.printStackTrace();
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErroException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/pay/product/findOne------" + e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(-1,e.getMessage());
        }
    }




}
